/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { hvigorCore } from '@ohos/hvigor';

import { TaskNames } from '../common/task-names.js';
import { ModuleTaskService } from '../service/module-task-service.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { SignHap } from '../sign-hap.js';

import { AbstractModuleHookTask } from './abstract-module-hook-task.js';
import CommonHookTask = TaskNames.CommonHookTask;
import { LegacyPackageHap } from '../legacy-tasks/legacy-package-hap.js';
import { PackageHap } from '../package-hap.js';

/**
 * 在Hsp/har中组装Hap打包的任务流,用于生成ohosTest的Hap包
 *
 * @since 2024/3/5
 */
export class GenOnDeviceTestHap extends AbstractModuleHookTask {
  constructor(moduleService: ModuleTaskService, isFaMode: boolean) {
    super(moduleService, isFaMode, CommonHookTask.GEN_ON_DEVICE_TEST_HAP);
  }

  initTaskDepends(taskTargetService: TargetTaskService) {
    if (this.skipSignHap(taskTargetService)) {
      this.dependsOn(
        this._isFaMode
          ? `${taskTargetService.getTargetData().getTargetName()}@${LegacyPackageHap.name}`
          : `${taskTargetService.getTargetData().getTargetName()}@${PackageHap.name}`,
      );
    } else {
      this.dependsOn(`${taskTargetService.getTargetData().getTargetName()}@${SignHap.name}`);
    }
  }

  /**
   * 是否跳过生成带签名的hap的步骤
   *
   * @private
   */
  private skipSignHap(taskTargetService: TargetTaskService) {
    return taskTargetService.getBuildOption().packOptions?.buildAppSkipSignHap && hvigorCore.isCommandEntryTask(TaskNames.CommonHookTask.ASSEMBLE_APP.name);
  }
}
